/* 通讯录管理系统 C++ 用struct 没有用class */

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

#define MAX 1000

struct person
{
    string m_Name;
    int m_Sex;
    int m_Age;
    string m_Phone;
    string m_Address;
};

const person nullContact =
    {
        "NULL",
        0,
        0,
        "NULL",
        "NULL",
};

struct contactBooks
{
    struct person personArray[MAX];
    int m_Size;
};

int isExist(const contactBooks &conB, const string &inputName);

void printMenu()
{
    cout << "***********************************************" << endl
         << "************ 1:new person: ********************" << endl // 1
         << "************ 2:del person: ********************" << endl // 2
         << "************ 3:display person: ****************" << endl // 3
         << "************ 4:search person: *****************" << endl // 4
         << "************ 5:modify person: *****************" << endl // 5
         << "************ 6:del all person: ****************" << endl // 6
         << "************ 0:Exit program: ******************" << endl // 0
         << "***********************************************" << endl;
}

void addPerson(contactBooks *conB)
{
    if (conB->m_Size >= MAX)
    {
        cout << "contackBook size is larger than 1000" << endl;
        return;
    }
    // name
    cout << "please input name:" << endl;
    string name;
    cin >> name;
    conB->personArray[conB->m_Size].m_Name = name;
    // sex
    cout << "please input sex: 0---boy, 1---girl" << endl;
    int sex;
    while (true)
    {
        cin >> sex;
        if (sex == 0 || sex == 1)
        {
            switch (sex)
            {
            case 0:
                conB->personArray[conB->m_Size].m_Sex = 0;
                break;
            case 1:
                conB->personArray[conB->m_Size].m_Sex = 1;
                break;
            default:
                break;
            }
            break;
        }
        else
        {
            cout << "please input correct num: " << endl;
        }
    }
    // age
    cout << "please input age:" << endl;
    int age = 0;
    cin >> age;
    conB->personArray[conB->m_Size].m_Age = age;
    // phone number
    cout << "please input phone number:" << endl;
    string phoneNum;
    cin >> phoneNum;
    conB->personArray[conB->m_Size].m_Phone = phoneNum;
    // address
    cout << "please input address:" << endl;
    string address;
    cin >> address;
    conB->personArray[conB->m_Size].m_Address = address;

    conB->m_Size += 1;
    cout << "add person finished!" << endl;
}

void delPerson(contactBooks *conB)
{

    string name;
    if (!conB->m_Size)
    {
        cout << "no person in contactBooks" << endl;
        return;
    }
    cout << "please input the person you want to delete:";
    cin >> name;
    for (size_t i = 0; i < conB->m_Size; ++i)
    {
        if (name == conB->personArray[i].m_Name)
        {
            for (size_t j = i; j < conB->m_Size; ++j)
            {
                conB->personArray[j] = conB->personArray[j + 1];
            }
            conB->personArray[conB->m_Size - 1] = nullContact;
            conB->m_Size -= 1;
            cout << "delete successfully!" << endl;
            break;
        }
        else
        {
            cout << "please input correct name" << endl;
        }
    }
}

void dispPerson(const contactBooks *conB)
{
    if (!conB->m_Size)
    {
        cout << "no person in contactBooks." << endl;
        return;
    }
    string sex;

    cout << "all contact person is listed as bellow:" << endl;
    for (size_t i = 0; i < conB->m_Size; i++)
    {
        sex = conB->personArray[i].m_Sex == 1 ? "girl" : "boy";
        cout << " name: " << conB->personArray[i].m_Name << " sex: " << sex << " age: "
             << conB->personArray[i].m_Age << " phoneNumber: " << conB->personArray[i].m_Phone << " addres: " << conB->personArray[i].m_Address << endl;
    }
}

void searchPerson(const contactBooks *conB)
{
    if (!conB->m_Size)
    {
        cout << "no person in contact!" << endl;
        return;
    }
    cout << "please input the name you want to search: " << endl;
    string name;
    cin >> name;
    size_t nameFoundNum = 0;
    for (size_t i = 0; i < conB->m_Size; i++)
    {
        if (name == conB->personArray[i].m_Name)
        {
            cout << " name: " << conB->personArray[i].m_Name << " sex: " << conB->personArray[i].m_Sex
                 << " age: " << conB->personArray[i].m_Age << " phoneNumber: " << conB->personArray[i].m_Phone
                 << " addres: " << conB->personArray[i].m_Address << endl;
            nameFoundNum++;
            break;
        }
    }
    if (!nameFoundNum)
    {
        cout << "Thers is no person name in contact!" << endl;
    }
    else
    {
        cout << nameFoundNum << "persons founded " << endl;
    }
}

void modifyPerson(contactBooks &conB)
{
    string name;
    cout << "Please input the person name you want to modify: " << endl;
    cin >> name;
    int index = isExist(conB, name);
    if (index != -1)
    {
        cout << "please input name: " << endl;
        cin >> conB.personArray[index].m_Name;
        cout << "please input sex: " << endl;
        cin >> conB.personArray[index].m_Sex;
        cout << "please input age: " << endl;
        cin >> conB.personArray[index].m_Age;
        cout << "please input phone: " << endl;
        cin >> conB.personArray[index].m_Phone;
        cout << "please input address: " << endl;
        cin >> conB.personArray[index].m_Address;
        cout << "modified successfully!" << endl;
    }
    else
    {
        cout << "the name you inputed is not exist! " << endl;
    }
}

void delAllPerson(contactBooks &conB)
{
    if (conB.m_Size == 0)
    {
        cout << "The contact is blank! " << endl;
        return;
    }
    cout << "y--definitaly delete, n--exit" << endl;
    char yn;
    cin >> yn;
    conB.m_Size = ('y' == yn) ? 0 : conB.m_Size;
    if (!conB.m_Size)
    {
        cout << "you have deleted all person in contact! " << endl;
    }
    else
    {
        cout << "you have exited!" << endl;
    }
}

int isExist(const contactBooks &conB, const string &inputName)
{
    for (size_t i = 0; i < conB.m_Size; i++)
    {
        if (inputName == conB.personArray[i].m_Name)
        {
            return i;
        }
    }
    return -1;
}

int main(int argc, char **argv)
{

    contactBooks niContack;
    niContack.m_Size = 0;

    int inputNum = 0;
    while (true)
    {
        printMenu();
        cout << "please input your choose num:";
        cin >> inputNum;
        switch (inputNum)
        {
        case 1:
            addPerson(&niContack);
            break;
        case 2:
            delPerson(&niContack);
            break;
        case 3:
            dispPerson(&niContack);
            break;
        case 4:
            searchPerson(&niContack);
            break;
        case 5:
            modifyPerson(niContack);
            break;
        case 6:
            delAllPerson(niContack);
            break;
        case 0:
            cout << "Exit!" << endl;
            return 0;
            break;
        default:
            break;
        }
        system("pause");
        system("cls");
    }

    system("pause");
    return 1;
}